package com.example.administrator.school.fragment;


import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.dmcbig.mediapicker.PickerConfig;
import com.dmcbig.mediapicker.PreviewActivity;
import com.dmcbig.mediapicker.entity.Media;
import com.example.administrator.school.App;
import com.example.administrator.school.R;
import com.example.administrator.school.adapter.FileAdapter;
import com.example.administrator.school.bean.MediaBean;
import com.example.administrator.school.db.MediaHelper;
import com.example.administrator.school.utils.OpenFileUtils;
import com.mingxiu.dialog.dialog.IOSAlert;
import com.xiumiing.uilibrary.base.BaseFragment;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

import static com.xiumiing.uilibrary.Constans.TITLE;
import static com.xiumiing.uilibrary.Constans.TYPE;

/**
 * A simple {@link Fragment} subclass.
 */
public class FileListFragment extends BaseFragment implements BaseQuickAdapter.RequestLoadMoreListener, SwipeRefreshLayout.OnRefreshListener, BaseQuickAdapter.OnItemClickListener, BaseQuickAdapter.OnItemLongClickListener {


    @BindView(R.id.recycler)
    RecyclerView mRecycler;
    @BindView(R.id.swipe_refresh)
    SwipeRefreshLayout mSwipeRefresh;
    Unbinder unbinder;
    private int mPage = 0;
    private FileAdapter mAdapter;
    private List<MediaBean> mData = new ArrayList<>();

    public static FileListFragment newInstance(String title, int type) {
        FileListFragment fragment = new FileListFragment();
        Bundle args = new Bundle();
        args.putString(TITLE, title);
        args.putInt(TYPE, type);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_file_list, container, false);
        unbinder = ButterKnife.bind(this, view);

        mSwipeRefresh.setOnRefreshListener(this);
        mSwipeRefresh.setColorSchemeColors(getResources().getColor(R.color.colorPrimary));

        setHotContent();

        return view;
    }

    /**
     * 内容
     */
    private void setHotContent() {
        mRecycler.setLayoutManager(new LinearLayoutManager(getContext()));
        mAdapter = new FileAdapter(mData);
        mAdapter.setOnItemClickListener(this);
        mAdapter.setOnItemLongClickListener(this);
        mAdapter.setOnLoadMoreListener(this, mRecycler);
        mRecycler.setAdapter(mAdapter);
        onRefresh();
    }

    @Override
    public void onRefresh() {
        mPage = 0;
        loadData();
    }

    @Override
    public void onLoadMoreRequested() {
        mPage++;
        loadData();
    }

    private void loadData() {
        if (mPage == 0) {
            mData.clear();
        }
        mRecycler.postDelayed(new Runnable() {
            @Override
            public void run() {
                FileFragment.getInstance().onRefresh();
                List<MediaBean> data = MediaHelper.getInstance().getData(mPage, mType);
                mData.addAll(data);
                mAdapter.notifyDataSetChanged();
                mSwipeRefresh.setRefreshing(false);
                if (data.size() < 10) {
                    mAdapter.loadMoreEnd();
                } else {
                    mAdapter.loadMoreComplete();
                }
            }
        }, 500);

    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {

        MediaBean mediaBean = mData.get(position);
        if (mediaBean.mediaType==2) {
            OpenFileUtils.openFile(mediaBean.path);
        }else {
            ArrayList<Media> media = new ArrayList<>();
            media.add(mediaBean.toMedia());
            Intent intent = new Intent(getActivity(), PreviewActivity.class);
            intent.putExtra(PickerConfig.MAX_SELECT_COUNT, 1);
            intent.putExtra(PickerConfig.PRE_RAW_LIST, media);
            this.startActivityForResult(intent, 200);
        }

    }

    @Override
    public boolean onItemLongClick(BaseQuickAdapter adapter, View view, final int position) {
        new IOSAlert.Builder(getActivity())
                .setTitle("提示")
                .setMessage("确定删除这条文件?")
                .setNegativeButton("取消")
                .setPositiveButton("确定", new IOSAlert.OnClickListener() {
                    @Override
                    public void onClick(IOSAlert iosAlert) {
                        MediaHelper.getInstance().delete(mData.get(position));
                        App.getInstance().showToast("删除成功");
                        onRefresh();
                    }
                })
                .show();
        return false;
    }
}
